﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="UserControlBase.cs" company="http://buginbox.wordpress.com/">
//   Copyright (c) GNU General Public License version 2 (GPLv2)
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;

namespace Wpf.Core.Control
{
    /// <summary>
    /// Base class for user controls
    /// </summary>
    public class UserControlBase : UserControl, IControlBase
    {
        #region UniqueId

        /// <summary>
        /// DependencyProperty to allow UniqueID to be defined as a binding property (To be available in ViewModel)
        /// </summary>
        public static readonly DependencyProperty UniqueIdProperty = DependencyProperty.Register("UniqueId", typeof(string), typeof(UserControlBase));

        /// <summary>
        /// Gets or sets unique identifier for this Control. Based on parents' controls names.
        /// </summary>
        public string UniqueId
        {
            get
            {
                string result = string.Empty;
                ControlHelper.UniqueIdParentRecursive(ref result, this);
                UniqueId = result;

                return (string)GetValue(UniqueIdProperty);
            }

            set
            {
                SetValue(UniqueIdProperty, value);
            }
        }

        #endregion UniqueId

        #region Order

        /// <summary>
        /// Gets or sets order when displayed in a PRISM region
        /// </summary>
        /// <value>
        /// The order. 
        /// </value>
        public int Order
        {
            get;
            set;
        }

        #endregion Order

        /// <summary>
        /// Determines whether [is in design mode] [the specified element].
        /// </summary>
        /// <param name="element">
        /// The element. 
        /// </param>
        /// <returns>
        /// <c>true</c> if [is in design mode] [the specified element]; otherwise, <c>false</c> . 
        /// </returns>
        protected static bool IsInDesignMode(DependencyObject element)
        {
            // Due to a known issue in Cider, GetIsInDesignMode attached property value is not enough to know if it's in design mode.
            return DesignerProperties.GetIsInDesignMode(element) || Application.Current == null || Application.Current.GetType() == typeof(Application);
        }
    }
}